#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 1e6 + 10, INF = 0x3f3f3f3f;

int n, m;
int block, t;
int st[N], ed[N], pos[N];
int a[N], add[N], sum[N];

void init()
{
    block = sqrt(n);
    t = (n + block - 1) / block;
    for (int i = 1;i <= t;i++) {
        st[i] = (i - 1) * block + 1;
        ed[i] = i * block;
    }
    ed[t] = n;
    for (int i = 1;i <= n;i++) pos[i] = (i - 1) / block + 1;
}

void change(int l, int r)
{
    int p = pos[l], q = pos[r];
    if (p == q) {
        for (int i = l;i <= r;i++) a[i] ^= 1;
        sum[p] = 0;
        for (int i = st[p];i <= ed[p];i++)
            sum[p] += a[i];
    } else {
        for (int i = p + 1;i <= q - 1;i++) add[i] ^= 1;

        for (int i = l;i <= ed[p];i++) a[i] ^= 1;
        sum[p] = 0;
        for (int i = st[p];i <= ed[p];i++) sum[p] += a[i];

        for (int i = st[q];i <= r;i++) a[i] ^= 1;
        sum[q] = 0;
        for (int i = st[q];i <= ed[q];i++) sum[q] += a[i];
    }
}

int query(int l, int r)
{
    int ans = 0;
    int p = pos[l], q = pos[r];
    if (p == q) {
        for (int i = l;i <= r;i++) ans += a[i] ^ add[p];
    } else {
        for (int i = p + 1;i <= q - 1;i++)
            ans += add[i] == 0 ? sum[i] : ed[i] - st[i] + 1 - sum[i];
        for (int i = l;i <= ed[p];i++)
            ans += a[i] ^ add[p];
        for (int i = st[q];i <= r;i++)
            ans += a[i] ^ add[q];
    }
    return ans;
}

void solve()
{
    cin >> n >> m;
    init();
    while (m--) {
        int op, l, r; cin >> op >> l >> r;
        if (op == 0) change(l, r);
        else cout << query(l, r) << "\n";
    }
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}